<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" >
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="请输入资讯标题关键词" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-date-picker v-model="dateArr" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format='yyyy-MM-dd HH:mm:ss'>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.author" placeholder="请输入发布者账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.recommend" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option v-for="item in option1" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="resetting()">重置</el-button>
        <el-button type="primary" @click="deleteHandle()">删除</el-button>
        <el-button type="primary" @click="addInformation()" v-if="isAuth('information:add')">+发布</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="序号">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建日期时间">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ getStrTime(scope.row.createTime) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="publicTime" header-align="center" align="center" label="发布日期">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ getStrTime(scope.row.publicTime) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" header-align="center" align="center" label="分类">
      </el-table-column>
      <el-table-column prop="author" header-align="center" align="center" label="发布者">
      </el-table-column>
      <el-table-column prop="messageScore" header-align="center" align="center" label="奖励积分">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.row.status == 1 ? '上架' : "下架" }}
          </div>
        </template>

      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="主推">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.row.recommend == 1 ? '是' : "否" }}
          </div>
        </template>

      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="活动">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.row.activityFlag == 1 ? '是' : "否" }}
          </div>
        </template>

      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <!-- <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >查看</el-button
          > -->
          <el-button type="text" size="small" v-if="isAuth('information:update')"
            @click="addInformation(scope.row.id)">编辑</el-button>
          <el-button type="text" v-if="isAuth('information:delete')" size="small"
            @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="cmtSet(scope.row.id, 0)"
            v-if="scope.row.status == 1 && isAuth('information:enable')">下架</el-button>
          <el-button type="text" size="small" @click="cmtSet(scope.row.id, 1)"
            v-if="scope.row.status == 0 && isAuth('information:disable')">上架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <el-dialog title="状态设置" :visible.sync="dialogVisible" width="30%">
      <el-form ref="dataForm" label-width="80px">
        <el-form-item label="资讯状态">
          <el-select v-model="informationStatus" placeholder="请选择">
            <el-option v-for="item in option1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cmtSet()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './informationList-add-or-update'
export default {
  data() {
    return {
      dateArr: [],
      informationStatus: false,
      dialogVisible: false,
      dataForm: {
        key: '',
        status: null,
        author: '',
        recommend: null,
      },
      option1: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '上架'
        },
        {
          value: 0,
          label: '下架'
        }
      ],
      options: [
        {
          value: null,
          label: '主推'
        }, {
          value: 1,
          label: '是'
        }, {
          value: 0,
          label: '否'
        }],
      // optionTwo: [
      //   {
      //     value: null,
      //     label: '全部'
      //   },{
      //     value: true,
      //     label: '启用'
      //   }, {
      //     value: false,
      //     label: '禁用'
      //   }],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      setId: ''
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },


  methods: {
    resetting() {
      this.dataForm = {
        key: '',
        author: '',
        status: null,
        recommend: null
      }
      this.dateArr = []
      this.getDataList()
    },
    showSet(id, e) {
      this.informationStatus = e
      this.setId = id
      this.dialogVisible = true
    },
    cmtSet(setId, informationStatus) {
      this.$http({
        url: this.$http.adornUrl(informationStatus == 1 ? '/admin/information/enable' : '/admin/information/disable'),
        method: 'POST',
        data: this.$http.adornData({ id: setId, status: informationStatus }, false)
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
              this.dialogVisible = false
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 获取数据列表
    getDataList() {
      var recommend = {}
      var status = {}
      this.dataListLoading = true
      if (this.dataForm.recommend !== null) {
        recommend.recommend = this.dataForm.recommend
      }
      if (this.dataForm.status !== null) {
        status.status = this.dataForm.status
      }
      let params = {
      }
      if (this.dateArr && this.dateArr.length > 0) {
        params.startTime = this.dateArr[0]
        params.endTime = this.dateArr[1]
      }
      this.$http({
        url: this.$http.adornUrl(`/admin/information/page`),
        method: 'post',
        data: this.$http.adornData({
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          author: this.author,
          ...recommend,
          'title': this.dataForm.key,
          // ...positionId,
          ...status,
          ...params
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 发布资讯
    addInformation(id) {
      this.$router.push({ name: 'i-createInformation', query: { id: id } })
    },
    // // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      let obj = {}

      var ids = this.dataListSelections.map((item) => {
        return item.id;
      })
      if (!id && !ids.length) {
        this.$message({
          message: '请选择要删除的选项',
          type: 'error',
          duration: 1500,
        })
        return
      }
      if (id) {
        obj = {
          id: id
        }
      } else {
        obj = {
          ids: ids
        }
      }
    this.$confirm(
      `确定进行删除操作?`,
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    ).then(() => {
      this.$http({
        url: this.$http.adornUrl('/admin/information/delete'),
        method: 'post',
        data: this.$http.adornData(obj)
      }).then(({ data }) => {
        if (data && data.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    })
  }
}
}
</script>
